/**
 * 
 */
package de.cbf.cam.map2d.shapeListeners;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;

import de.cbf.cam.map2d.listeners.ShapeHoverListener;
import de.cbf.cam.map2d.listeners.ShapeSelectionListener;
import de.cbf.cam.map2d.shapes.Shape;
import de.cbf.cam.map2d.shapes.ShapePlayer;

/**
 * @author Stefan Alter
 *
 */
public class PlayerShapeListener implements ShapeSelectionListener,ShapeHoverListener{
	private Object character;
	private PaintListener hoverPaintListener; 
	private PaintListener selectionPaintListener; 
	
	public PlayerShapeListener(Object character) {
		super();
		this.character = character;
	}
	
	@Override
	public void onHover(Point position, Canvas paintArea,Shape shape) {
		final int x = position.x;
		final int y = position.y;
		//remove previous listener
		paintArea.removePaintListener(hoverPaintListener);
		//display new at new position
		hoverPaintListener= new PaintListener() {				
			@Override
			public void paintControl(PaintEvent e) {
				e.gc.drawText(character.toString(),x,y);					
			}
		};
		paintArea.addPaintListener(hoverPaintListener);
	}
	
	@Override
	public void onSelection(Canvas paintArea,Shape shape) {			
		//remove previous listener
		paintArea.removePaintListener(selectionPaintListener);
		if (shape instanceof ShapePlayer) {
			final ShapePlayer shapePlayer = (ShapePlayer) shape;
			selectionPaintListener= new PaintListener() {				
				@Override
				public void paintControl(PaintEvent e) {
					e.gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
					e.gc.drawOval(shapePlayer.getMiddle().x,shapePlayer.getMiddle().y, shapePlayer.getRadius()+2,shapePlayer.getRadius()+2);					
				}
			};
		}
		//display new at new position			
		paintArea.addPaintListener(selectionPaintListener);			
	}

	@Override
	public void removePaint(Canvas paintArea) {
		paintArea.removePaintListener(selectionPaintListener);
		paintArea.removePaintListener(hoverPaintListener);
		
	}		
}
